package com.ls.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.ls.pojo.SysRole;
import com.ls.pojo.SysUser;
import com.ls.service.sysRole.SysRoleService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping(value = "/sys/role")
public class SysRoleController {
    private Logger logger = Logger.getLogger(SysRoleController.class);

    @Resource
    private SysRoleService sysRoleService;

    @GetMapping(value = "/list")
    public String list(Model model) throws Exception {
        List<SysRole> roleList = sysRoleService.getRoleList();
        model.addAttribute("roleList", roleList);

        return "sysRole/list";
    }

    @ResponseBody
    @GetMapping(value = "/simpleList")
    public Object simpleList() throws Exception {
        List<SysRole> roleList = sysRoleService.getRoleList();

        return JSONArray.toJSONString(roleList);
    }

    @GetMapping(value = "/toAdd")
    public String toAdd(@ModelAttribute(value = "role") SysRole sysRole) {
        return "sysRole/add";
    }

    @PostMapping(value = "/add")
    public String add(SysRole sysRole, HttpSession session) throws Exception {
        sysRole.setCreatedUserId(((SysUser)session.getAttribute("userSession")).getId());

        return sysRoleService.add(sysRole) ? "redirect:sys/role/list" : "sysRole/add";
    }

    @GetMapping(value = "/toUpdate/{id}")
    public String toUpdate(@PathVariable Integer id, HttpServletRequest request, Model model) throws Exception {
        SysRole sysRole = sysRoleService.getById(id);
        model.addAttribute(sysRole);

        return "sysRole/update";
    }

    @PostMapping(value = "/update")
    public String update(SysRole sysRole, HttpSession session) throws Exception {
        sysRole.setUpdatedUserId(((SysRole)session.getAttribute("userSession")).getId());

        return sysRoleService.update(sysRole) ? "redirect:/sys/role/list" : "sysRole/update";
    }

    @ResponseBody
    @RequestMapping(value = "/codeExist")
    public Object codeExist(@RequestParam String code) throws Exception {
        HashMap resultMap = new HashMap();

        if (code != null && !code.trim().equals("")) {
            int count = sysRoleService.getCountByCode(code);

            if (count == 0) {
                resultMap.put("exist", 0);
            } else {
                resultMap.put("exist", 1);
            }
        } else {
            resultMap.put("exist", -1);
            resultMap.put("msg", "角色编号不可以为空");
        }

        return JSONArray.toJSONString(resultMap);
    }

    @DeleteMapping(value = "/id/{id}")
    public String del(@PathVariable Integer id) {
        HashMap resultMap = new HashMap();

        try {
            if (sysRoleService.deleteById(id)) {
                resultMap.put("delResult", "true");
            } else {
                resultMap.put("delResult", "false");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return JSON.toJSONString(resultMap);
    }
}